"""
增加一些测试数据
"""

import shutil
from flask import Flask, request, jsonify, send_from_directory, session, redirect, url_for
import os
from multiprocessing import Queue, freeze_support
from func.dao import Database
from werkzeug.security import generate_password_hash, check_password_hash
from func.user_service import (
    is_valid_email, register_user, login_user, get_user_profile as get_user_profile_service,
    get_user_addresses, add_user_address, update_user_address, delete_user_address,
    get_address_by_id, update_user_avatar, update_user_password, update_user_description
)
from func.product_service import (
    get_products_by_page, search_products, get_product_details,
    get_user_products, get_user_favorites, get_user_transactions,
    get_product_comments, add_product_comment, delete_product_comment,
    toggle_product_favorite, get_product_images, add_product_image, delete_product_image,
    add_product
)
from func.email_service import send_verification_code, verify_code
from flask_cors import CORS
from werkzeug.utils import secure_filename
import time

test_user = {
    "email": "test@stu.pku.edu.cn",
    "password": "test123456",
    "nickname": "test",
    "description": "test"
}

# 初始化数据库路径
from config import DB_PATH

db = None

# 创建数据目录
if not os.path.exists('data'):
    os.makedirs('data')

# 注册测试用户
def register_test_user():
    hashed_password = generate_password_hash(test_user["password"])
    register_user(db, test_user["email"], hashed_password, test_user["nickname"])

# 修改描述
def update_test_user_description():
    update_user_description(db, test_user["email"], "test123456")

# 用户发布商品
def test_publish_product():
    test_products = [
        {
            "name": f"test{i}",
            "description": f"test{i}",
            "price": 100,
            "image_folder": f"images/products/{i}"
        } for i in range(1, 101)
    ]
    for product in test_products:
        add_product(db, test_user["email"], product["name"], product["description"], product["price"], product["image_folder"])
    for product in test_products:
        image_folder = os.path.join("static", product["image_folder"])
        if not os.path.exists(image_folder):
            os.makedirs(image_folder)


# 用户为商品上传图片
def test_upload_product_image():
    for i in range(1, 101):
        add_product_image(db, i, f"images/bike2.jpg")
        # 将static/images/default_head.jpg复制到static/images/products/{i}/
        # shutil.copy("static/images/default_head.jpg", f"static/images/products/{i}/1.jpg")

if __name__ == '__main__':
    freeze_support()  # 添加这行来支持Windows下的多进程

    # 启动DAO进程
    db = Database(DB_PATH)

    try:
        register_test_user()
    except Exception as e:
        print(e)
    try:
        update_test_user_description()
    except Exception as e:
        print(e)
    try:
        test_publish_product()
    except Exception as e:
        print(e)
    try:
        test_upload_product_image()
    except Exception as e:
        print(e)
    # 关闭DAO进程
